//
//  SelectedNumberTableViewCell.m
//  DayAndDayLottery
//
//  Created by dengzhiping on 2017/5/27.
//  Copyright © 2017年 dengzhiping. All rights reserved.
//

#import "SelectedNumberTableViewCell.h"
#import "SelectedNumberView.h"
#define BUTTONTAG 1000

@interface SelectedNumberTableViewCell(){
    NSInteger _numCount;
}
@property (nonatomic, strong) UIButton *tempBtn;
@property (nonatomic, strong) NSMutableArray *seletedNumArray;
@end

@implementation SelectedNumberTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    
    self.seletedNumArray = [NSMutableArray array];
    _buyCount = 0;
    UIView *selectedNumView = [[UIView alloc] init];
    [self.contentView addSubview:selectedNumView];
    [selectedNumView remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(44);
        make.right.equalTo(-35);
        
    }];
    
    for (int i=0; i<11; i++) {
        SelectedNumberView *numView = [SelectedNumberView initSelectedNumberViewFromXib];
        [selectedNumView addSubview:numView];
        numView.tag = BUTTONTAG + i;
        numView.block = ^(UIButton *btn) {
            if (btn.isSelected) {
                _buyCount +=1;
                [self.seletedNumArray addObject:btn.titleLabel.text];
            }else{
                _buyCount -=1;
                for (int j=0; j<self.seletedNumArray.count; j++) {
                    if ([btn.titleLabel.text isEqualToString:self.seletedNumArray[j]]) {
                        [self.seletedNumArray removeObjectAtIndex:j];
                    }
                }

            }
            NSNotification * notice = [NSNotification notificationWithName:@"buyCount" object:nil userInfo:@{@"buyCount":self.seletedNumArray}];
            [[NSNotificationCenter defaultCenter]postNotification:notice];
        };
        
        [numView viewWithButtonNumTitle:i+1<10?[NSString stringWithFormat:@"0%d",i+1]:[NSString stringWithFormat:@"%d",i+1] bottomLabelTitle:[NSString stringWithFormat:@"%d",i]];
        
    }
    
    [selectedNumView.subviews mas_distributeSudokuViewsWithFixedItemWidth:40 fixedItemHeight:70
                                                         fixedLineSpacing:0 fixedInteritemSpacing:20
                                                                warpCount:5
                                                               topSpacing:0
                                                            bottomSpacing:0 leadSpacing:20 tailSpacing:10];
    
}

- (void)clearSelectedNumber{
    _buyCount = 0;
    for (int i=0; i<11; i++) {
        SelectedNumberView *numView = [self viewWithTag:1000 + i];
        numView.selectedButton.selected = NO;
    }
}

# pragma mark - 生成随机数
- (NSArray *)randomNumerWithCount:(NSInteger)count {
    
    if (count == 0) {
        [self clearSelectedNumber];
        return nil;
    }
    
    [self clearSelectedNumber];
    NSArray *allNumArray = @[@(1),@(2),@(3),@(4),@(5),@(6),@(7),@(8),@(9),@(10),@(11)];
    NSMutableArray *allNumMutArray = [NSMutableArray array];
    [allNumMutArray addObjectsFromArray:allNumArray];
    
    NSMutableArray *randomArray = [NSMutableArray array];
    
    for (int i=0; i<999; i++) {
        int x = arc4random() % allNumArray.count;
        [allNumMutArray enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            if ([@(x) isEqual:obj]) {
                [allNumMutArray removeObjectAtIndex:idx];
                [randomArray addObject:obj];
                
            }
        }];
        
        if (randomArray.count == count) {
            _numCount = count;
            break;
        }
    }
    return randomArray;
}

- (IBAction)randomChooseButton:(UIButton *)sender {
    
    [self clearSelectedNumber];
    [self.seletedNumArray removeAllObjects];
    NSArray *array  = [self randomNumerWithCount:_numCount];
    for (int i=0; i<array.count;i++) {
        int temp = [[NSString stringWithFormat:@"%@",array[i]] intValue];
        SelectedNumberView *numView = [self viewWithTag:1000 + temp];
        [numView.selectedButton setSelected:YES];
        NSLog(@"%@",numView.selectedButton.titleLabel.text);
        [self.seletedNumArray addObject:numView.selectedButton.titleLabel.text];
        if (self.tempBtn == nil) {
            self.tempBtn = numView.selectedButton;
            _buyCount +=1;
            
        }else{
            if (![numView.selectedButton isEqual:self.tempBtn]) {
                _buyCount +=1;
                self.tempBtn = numView.selectedButton;
            }else{
                // NSLog(@"相同");
                self.tempBtn = numView.selectedButton;
                
            }
        }
    }
    NSNotification * notice = [NSNotification notificationWithName:@"buyCount" object:nil userInfo:@{@"buyCount":self.seletedNumArray}];
    [[NSNotificationCenter defaultCenter]postNotification:notice];

}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
